package com.rp.system.user.controller;

import com.rp.common.aspect.annotetion.AutoLog;
import com.rp.common.aspect.annotetion.ResponseResultBody;
import com.rp.common.config.shiro.RetryLimitHashedCredentialsMatcher;
import com.rp.common.config.shiro.ShiroConfig;
import com.rp.common.result.ResultDTO;
import com.rp.common.utils.IPUtils;
import com.rp.common.utils.Md5PassWordUtil;
import com.rp.common.config.redis.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 类说明:
 *
 * @author ****
 * @date 2020-12-23 10:23  新建
 */
@Slf4j
@RestController
@RequestMapping("/sys-utils")
@ResponseResultBody
@Api(tags = "系统工具接口")
public class SysUtilsController {

    @Autowired
    private RetryLimitHashedCredentialsMatcher retryLimitHashedCredentialsMatcher;

    @Autowired
    private RedisUtil redisUtil;

    @AutoLog(value = "系统工具接口-解除用户冻结")
    @ApiOperation(value = "解除用户冻结 方便测试")
    @GetMapping("/unlockAccount")
    public ResultDTO unlockAccount(String username) {
        retryLimitHashedCredentialsMatcher.unlockAccount(username);
        return ResultDTO.success();
    }

    @AutoLog(value = "系统工具接口-获取shiroMD5加密后的密码")
    @ApiOperation(value = "获取shiroMD5加密后的密码")
    @GetMapping("/getMd5PassWord")
    public ResultDTO getMd5PassWord(String username, String password, String salt) {
        return ResultDTO.success().data(Md5PassWordUtil.getMd5(password, username + salt, ShiroConfig.HASH_ITERATIONS));
    }

    @AutoLog(value = "系统工具接口-解除接口访问限制")
    @ApiOperation(value = "解除接口访问限制")
    @GetMapping("/unfreeze")
    public ResultDTO unfreeze(HttpServletRequest httpRequest) {
        String ip = IPUtils.getIpAddr(httpRequest);
        redisUtil.hdel("filter:ip:black", ip);
        return ResultDTO.success().message("已解除IP:" + ip + "接口访问限制");
    }
}
